package com.kylin.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//练习Thread，实现多线程同步下载图片
public class TestThread02 extends Thread {

    private String url;//网络图片地址
    private String name;//保存的文件名

    public TestThread02(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {

        WebDownloader1 webDownloader1 = new WebDownloader1();
        webDownloader1.downloader(url, name);
        System.out.println("下载了文件名为" + name);
    }

    public static void main(String[] args) {
        TestThread02 t1 = new TestThread02("https://cdn.jsdelivr.net/gh/kylincw/img/img/%E5%AE%87%E8%88%AA%E5%91%98.png", "1.png");
        TestThread02 t2 = new TestThread02("https://cdn.jsdelivr.net/gh/kylincw/img/img/%E5%AE%87%E8%88%AA%E5%91%98.png", "2.png");
        TestThread02 t3 = new TestThread02("https://cdn.jsdelivr.net/gh/kylincw/img/img/%E5%AE%87%E8%88%AA%E5%91%98.png", "3.png");

        t1.start();
        t2.start();
        t3.start();
    }
}


//下载器
class WebDownloader1 {
    //下载方法
    public void downloader(String url, String name) {

        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，downloader方法出现问题");
        }
    }
}

